/*
	Azid Engine is 3D Game Engine developed along side with Galactic Siege (see Google Code) 
	Copyright (C) 2010  Jorgen Ader

	This file is part of AzidEngine.

	AzidEngine is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	AzidEngine is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with AzidEngine.  If not, see <http://www.gnu.org/licenses/>. 
*/

#include "../includes/EngineStateManager.hpp"

using namespace AzidEngine;

StateManager::StateManager()
{
}

StateManager::~StateManager()
{
	// TODO add deletion of State's that were not removed automaticly
}

void StateManager::onCurrentUpdate()
{
	mCurrentStatePtr->StatePtr->onUpdate();
}

bool StateManager::setCurrentState(EngineState* state, Ogre::String stateName)
{
	if(mCurrentStatePtr == NULL && state == NULL && stateName == "")
		return false;
	if(mCurrentStatePtr != NULL || (mCurrentStatePtr == NULL && stateName != ""))
		mCurrentStatePtr->StatePtr->destroy();
	std::vector<StateInfo>::iterator it;
	for(it = mStateList.begin(); it != mStateList.end(); it++)
	{
		if(*it->StatePtr == state)
		{
			mCurrentStatePtr = it;
			break;
		}
	}
	mCurrentStatePtr->StatePtr->onCreate();
	return true;
}

void StateManager::onDestroyCurrentState()
{
	mCurrentStatePtr->StatePtr->onDestroy();
}

void StateManager::addState(EngineState* state)
{
	StateInfo si;
	si.StatePtr = state;
	mStateList.push_back(si);
}

void StateManager::removeState(EngineState* state)
{
	std::vector<StateInfo>::iterator it;
	for(it = mStateList.begin(); it != mStateList.end(); it++)
	{
		if((state != NULL && *it->StatePtr == state) || (state == NULL && *it->StateName == stateName))
			mStateList.erase(it);
	}
}
